package com.tql.pandian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.constant.CommonUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.datasource.annotation.Slave;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.tql.pandian.entity.analysis.dto.PanDianCodeDTO;
import com.tql.pandian.entity.analysis.vo.FoodReportVo;
import com.tql.pandian.entity.dto.EndInventoryDTO;
import com.tql.pandian.entity.dto.ProductPositiveDTO;
import com.tql.pandian.entity.vo.*;
import com.tql.pandian.mapper.PanDianAnalysisMapper;
import com.tql.pandian.service.ICommonService;
import com.tql.pandian.service.IPanDianAnalysisService;
import com.tql.pandian.util.CommonUtilHll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

@Service
public class PanDianAnalysisService implements IPanDianAnalysisService {

    @Autowired
    private PanDianAnalysisMapper panDianAnalysisMapper;

    @Autowired
    private ICommonService commonService;

    @Override
    public List<MouthVo> getByMoth(String byMonth) {
        List<MouthVo> mapList = panDianAnalysisMapper.selectByMoth(byMonth);
        return mapList;
    }

    @Override
    public List<PanDianCodeVo> getPanDianCode(PanDianCodeDTO dto) {
        return panDianAnalysisMapper.selectPanDianCode(dto);
    }

    @Override
    public List<EndInventoryVo> getEndInventory(EndInventoryDTO endInventoryDTO) {
        String deptCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
        boolean flag = SecurityUtils.isAdmin(SecurityUtils.getUserId());
//        PageHelper.startPage(endInventoryDTO.getPageNum(),endInventoryDTO.getPageSize());
        if (!flag) {
            endInventoryDTO.setDeptCode(deptCode);
        }
        return panDianAnalysisMapper.selectEndInventory(endInventoryDTO);
    }


    @Override
    public Integer getEndInventoryCount(EndInventoryDTO endInventoryDTO) {
        String deptCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
        boolean flag = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        PageHelper.startPage(endInventoryDTO.getPageNum(), endInventoryDTO.getPageSize());
        if (!flag) {
            endInventoryDTO.setDeptCode(deptCode);
        }
        return panDianAnalysisMapper.selectEndInventoryCount(endInventoryDTO);

    }

    @Override
    @Slave
    public List<ProductPositiveVo> getProductPositive(ProductPositiveDTO dto) throws ParseException {
        int month = CommonUtilHll.getMonth(dto.getParamTime());
        String tabName = CommonUtil.TABLE_BILL_FOOD_REPORT + month;
        return panDianAnalysisMapper.selectProductPositive1(dto.getParamTime(),  dto.getShopId(), tabName);
    }

    @Override
    @Slave

    public List<ProductPositiveProfitVo> getProductPositiveProfit(ProductPositiveDTO dto) throws ParseException {


        int month = CommonUtilHll.getMonth(dto.getParamTime());
        return getProductPositiveProfit1(dto.getParamTime(), month, dto.getShopId());

        // return panDianAnalysisMapper.selectProductPositiveProfit(dto);

    }

    @Slave
    private List<ProductPositiveProfitVo> getProductPositiveProfit1(String paramTime, int month, String shopId1) {

        String tabName = CommonUtil.TABLE_BILL_FOOD_REPORT + month;
        return panDianAnalysisMapper.selectProductPositiveProfit1(paramTime, shopId1, tabName);

    }
}